package jacidco;
import java.lang.Exception;
/**
 * Frame parala creacion de los Pagos que realizan los Afiliados a la Comunidad
 * @author David Cuellar
 */
public class CreaPagoFrame extends javax.swing.JFrame implements BotonesNuevoCrearCancelar
{

    /** Constructor de Frame */
    public CreaPagoFrame() 
    {
        initComponents();
        
        Main.bd.conectar();
        txtCodPago.setText(""+(Main.bd.nextCode("cod_pago", "pagos")+1));
        Main.bd.cerrarConexion();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblMarcareg = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lblTitulo4 = new javax.swing.JLabel();
        btmCrear = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnNuevo = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        lblCodPago = new javax.swing.JLabel();
        txtCodPago = new javax.swing.JTextField();
        lblNombrePago = new javax.swing.JLabel();
        txtNombrePago = new javax.swing.JTextField();
        lblDescripcionPago = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaDescripcionPago = new javax.swing.JTextArea();
        lblValor = new javax.swing.JLabel();
        txtValor = new javax.swing.JTextField();
        lblTitulo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblMarcareg.setFont(new java.awt.Font("Times New Roman", 0, 11)); // NOI18N
        lblMarcareg.setText("Copyright 2011. DCM. All Rights Reserved");
        lblMarcareg.setName("lblMarcareg"); // NOI18N

        jLabel1.setBackground(new java.awt.Color(255, 51, 51));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jacidco/resources/Logo_Jacidco_mini.png"))); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        lblTitulo4.setFont(new java.awt.Font("Times New Roman", 3, 36)); // NOI18N
        lblTitulo4.setText("Pagos");
        lblTitulo4.setName("lblTitulo4"); // NOI18N

        btmCrear.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        btmCrear.setText("Crear");
        btmCrear.setName("btmCrear"); // NOI18N
        btmCrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btmCrearActionPerformed(evt);
            }
        });

        btnCancelar.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.setName("btnCancelar"); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnNuevo.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        btnNuevo.setText("Nuevo");
        btnNuevo.setName("btnNuevo"); // NOI18N
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setName("jPanel1"); // NOI18N

        lblCodPago.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        lblCodPago.setText("Codigo Pago :");
        lblCodPago.setName("lblCodPago"); // NOI18N

        txtCodPago.setEditable(false);
        txtCodPago.setName("txtCodPago"); // NOI18N

        lblNombrePago.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        lblNombrePago.setText("Nombre :");
        lblNombrePago.setName("lblNombrePago"); // NOI18N

        txtNombrePago.setName("txtNombrePago"); // NOI18N

        lblDescripcionPago.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        lblDescripcionPago.setText("Descripcion :");
        lblDescripcionPago.setName("lblDescripcionPago"); // NOI18N

        jScrollPane1.setToolTipText("");
        jScrollPane1.setAutoscrolls(true);
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txaDescripcionPago.setColumns(20);
        txaDescripcionPago.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
        txaDescripcionPago.setLineWrap(true);
        txaDescripcionPago.setRows(5);
        txaDescripcionPago.setName("txaDescripcionPago"); // NOI18N
        jScrollPane1.setViewportView(txaDescripcionPago);

        lblValor.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        lblValor.setText("Valor :");
        lblValor.setName("lblValor"); // NOI18N

        txtValor.setName("txtValor"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblCodPago)
                    .addComponent(lblNombrePago)
                    .addComponent(lblDescripcionPago)
                    .addComponent(lblValor))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtNombrePago, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtCodPago, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))
                    .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(41, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCodPago)
                    .addComponent(txtCodPago, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombrePago)
                    .addComponent(txtNombrePago, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDescripcionPago)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblValor))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        lblTitulo.setFont(new java.awt.Font("Times New Roman", 3, 18));
        lblTitulo.setText("Comunidad Dinamica en Java");
        lblTitulo.setName("lblTitulo"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTitulo4))
                .addContainerGap(65, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(217, Short.MAX_VALUE)
                .addComponent(btnNuevo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btmCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelar)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblMarcareg)
                .addContainerGap(245, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblTitulo)
                        .addGap(67, 67, 67)
                        .addComponent(lblTitulo4)))
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btmCrear)
                    .addComponent(btnNuevo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblMarcareg)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btmCrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btmCrearActionPerformed
        
        try
        {
            capturaDatos();
            guardaDatos();
        }
        catch(Exception ex)
        {
            if(ex.toString().contains("java.lang.NumberFormatException: For input string:"))
                Main.mensaje.advertencia("El campo valor debe ser numerico");
            
        }
        
        
    }//GEN-LAST:event_btmCrearActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        
        Main.goToMenuPrincipal();
        dispose();
        
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        
        limpiarCampos();
        
    }//GEN-LAST:event_btnNuevoActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btmCrear;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCodPago;
    private javax.swing.JLabel lblDescripcionPago;
    private javax.swing.JLabel lblMarcareg;
    private javax.swing.JLabel lblNombrePago;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JLabel lblTitulo4;
    private javax.swing.JLabel lblValor;
    private javax.swing.JTextArea txaDescripcionPago;
    private javax.swing.JTextField txtCodPago;
    private javax.swing.JTextField txtNombrePago;
    private javax.swing.JTextField txtValor;
    // End of variables declaration//GEN-END:variables

    
    /**
     * Captura los datos ingresados en los JTaxtField y los almacena en el objeto pago
     * @see Pago
     */
    public boolean capturaDatos() 
    {
        Main.pago.setCod(Byte.parseByte(txtCodPago.getText()));
        Main.pago.setNombre(txtNombrePago.getText());
        Main.pago.setDescription(txaDescripcionPago.getText());
        Main.pago.setValor(Integer.parseInt(txtValor.getText()));
        return true;
    }

     /**
     * Realiza la validacion de campos vacios y envias los datos almacenados en el objeto pago
     * al metodo insertPago del objeto bd.
     * @see Pago
     * @see BaseDatos
     */
    public boolean guardaDatos() 
    {
        Main.bd.conectar();
        if(!txtNombrePago.getText().equals("") && !txaDescripcionPago.getText().equals("") && !txtValor.getText().equals(""))
        {     
            if(Main.bd.guardaDatosPago(Main.pago.getCod(), Main.pago.getNombre(), Main.pago.getDecription(), Main.pago.getValor()))
            {
                Main.mensaje.datosGuardaosOK("Creacion de Pago");
                txtNombrePago.setEnabled(false);
                txaDescripcionPago.setEnabled(false);
                txtValor.setEnabled(false);
            }   
        }
        else
            Main.mensaje.noCamposVacios();
        
        Main.bd.cerrarConexion();
        return true;
    }

    /**
    * Limpia y habilita los campos para registrar un nuevo Pago
    */
    public boolean limpiarCampos() 
    {
        Main.bd.conectar();
        txtCodPago.setText(""+(Main.bd.nextCode("cod_pago", "pagos")+1));
        Main.bd.cerrarConexion();
        
        txtNombrePago.setText("");
        txtNombrePago.setEnabled(true);
        txtNombrePago.requestFocus();
        txaDescripcionPago.setText("");
        txaDescripcionPago.setEnabled(true);
        txtValor.setText("");
        txtValor.setEnabled(true);
        return true;
        
    }
}
